﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using Common.Model;
using Common.util;
using Dapper;
using log4net;
using MoreLinq;

namespace Common.Dao
{
    public class DailyKLineDao
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(DailyKLine));

        public static void UpdateOrAddDailyKLine(List<DailyKLine> dailyKLines)
        {
            int batchSize = 10000;
            var batchData = dailyKLines.Batch(batchSize);
            foreach (var data in batchData)
            {
                var sql = new StringBuilder("REPLACE INTO daily_kline (stock_code,update_time,close_price,volume,open_price)  VALUES ");
                bool first = true;
                foreach (var kLine in data)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        sql.Append(",");
                    }

                    sql.Append($"('{kLine.StockCode}','{kLine.UpdateTime}','{kLine.ClosePrice}','{kLine.Volume}','{kLine.OpenPrice}')");
                }

                using (var cnn = DbUtils.GetHotTopicConnection())
                {
                    cnn.Execute(sql.ToString());
                    Log.Info("insert block_daily_kline data");
                }
            }
        }

        public static List<DailyKLine> GetDailyKLines(string stockCode,DateTime endDate,int count,bool includeEndDate)
        {
            var sql = "SELECT stock_code AS StockCode,close_price AS ClosePrice,update_time AS UpdateTime,volume,open_price AS OpenPrice "
                      + "FROM daily_kline WHERE stock_code = @stockCode "
                      + "AND update_time " + (includeEndDate ? "<=" : "<") + " @endDate "
                      + "ORDER BY update_time DESC "
                      + "LIMIT " + count;
            using (var cnn = DbUtils.GetHotTopicConnection())
            {
                return cnn.Query<DailyKLine>(sql, new { stockCode, endDate = endDate.ToShortDateString().Replace("/","-")})?.ToList();
            }
        }

        public static List<ZdfModel> GetZdf(DateTime beginDate, DateTime endDate)
        {
            var sql = " SELECT a.stock_code as StockCode, (b.close_price-a.close_price)/a.close_price as Zdf " +
                      " FROM daily_kline a " +
                      " inner join daily_kline b on a.stock_code = b.stock_code " +
                      " where a.update_time = @beginDate and b.update_time = @endDate ";
            using (var cnn = DbUtils.GetHotTopicConnection())
            {
                return cnn.Query<ZdfModel>(sql, new {beginDate, endDate})?.ToList();
            }
        }
        
        public static List<LbMode> GetCjl(DateTime beginDate, DateTime endDate)
        {
            var sql = " SELECT stock_code AS StockCode,volume AS Volume " +
                      " FROM daily_kline "+
                      " WHERE update_time >= @beginDate and update_time <= @endDate";
            using (var cnn = DbUtils.GetHotTopicConnection())
            {
                return cnn.Query<LbMode>(sql, new {beginDate, endDate})?.ToList();
            }
        }
    }
}